package com.dmooo.yxmmerchant.view.model;

import android.content.Context;

import com.common.net.entity.SubjectPost;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.mywidget.LoadingDialog;
import com.common.net.subscribers.ProgressSubscriber;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.domain.MerchantInfoBean;
import com.dmooo.yxmmerchant.http.HttpManager;
import com.dmooo.yxmmerchant.view.contract.MainContract;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

public class MainModel implements MainContract.MainMdl {
    private Context context;
    private LoadingDialog loadingDialog;
    public MainModel(Context context) {
        this.context=context;
        loadingDialog=new LoadingDialog(context);
    }

    @Override
    public void getMerchantInfo(String token, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,false,false,"获取验证码"),map);
        HttpManager.getInstance().getMerchantMsg(post);
    }

    @Override
    public void editMerchantInfoBean(String token,MerchantInfoBean merchantInfoBean, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("service_tel",merchantInfoBean.merchant_detail.service_tel);
        map.put("service_area",merchantInfoBean.merchant_detail.service_area);
        map.put("province",merchantInfoBean.merchant_detail.province);
        map.put("city",merchantInfoBean.merchant_detail.city);
        map.put("county",merchantInfoBean.merchant_detail.county);
        map.put("detail_address",merchantInfoBean.merchant_detail.detail_address);
        map.put("lng_lat",merchantInfoBean.merchant_detail.lng_lat);
//        map.put("business_day_begin",merchantInfoBean.merchant_detail.business_day_begin);
//        map.put("business_day_end",merchantInfoBean.merchant_detail.business_day_end);
        map.put("business_hours_begin",merchantInfoBean.merchant_detail.business_hours_begin);
        map.put("business_hours_end",merchantInfoBean.merchant_detail.business_hours_end);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,false,false,"获取验证码"),map);
        HttpManager.getInstance().editMerchantMsg(post);
    }

    @Override
    public void editMerchantAvatar(String token, File file, final HttpOnNextListener listener) {
        try {
            RequestParams requestParams = new RequestParams();
            AsyncHttpClient client1 = new AsyncHttpClient();
            client1.addHeader("token", token);
            requestParams.put("token", token);
            requestParams.put("merchant_avatar", file);
            client1.post(Config.APP_INTERFACE_BASE_URL + "/merchant.php?c=Merchant&a=editMerchantAvatar", requestParams, new TextHttpResponseHandler() {

                @Override
                public void onStart() {
                    super.onStart();
                    loadingDialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    loadingDialog.dismiss();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    listener.onFail(responseString);
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    LogUtil.e("backdata",responseString);
                    try {
                        JSONObject object1=new JSONObject(responseString);
                        if(object1.getInt("code")==0){
                            listener.onNext("");
                        }else{
                            listener.onFail(object1.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }catch (FileNotFoundException e){
            LogUtil.e("backdata",e.toString());
        }
    }
}
